//
//  JZMenuViewController.m
//  QuanZiChat
//
//  Created by lanou3g on 15/11/23.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "JZMenuViewController.h"
#import <AVOSCloud.h>
#import "JZLoginViewController.h"
@interface JZMenuViewController ()<RCIMConnectionStatusDelegate,RCIMUserInfoDataSource>

@end

@implementation JZMenuViewController


- (void)viewDidAppear:(BOOL)animated{
    
    //判断是否存在缓存用户，当为空时，跳转到登录界面
    if ([AVUser currentUser] == nil) {
        UIStoryboard *login = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
        UINavigationController *loginVC = [login instantiateViewControllerWithIdentifier:@"UINavigationController"];
        [self presentViewController:loginVC animated: NO completion:nil];
    }
    
    AVUser *user = [AVUser currentUser];
    [[RCIM sharedRCIM] connectWithToken:[user objectForKey:@"Token"] success:^(NSString *userId) {
        
    } error:^(RCConnectErrorCode status) {
        NSLog(@"连接出现错误：原因：%ld",status);
    } tokenIncorrect:^{
        NSLog(@"token无效");
    }];

    [super viewDidAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置代理
    [[RCIM sharedRCIM] setConnectionStatusDelegate:self];
    [[RCIM sharedRCIM] setUserInfoDataSource:self];

}

// 网络状态监测
- (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status{
    if (status == ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT) {
        [self alert:@"您的账号在别的设备上登陆，您被迫下线!"];
    }
}

- (void)alert:(NSString *)msg{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"知道了" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        [[RCIM sharedRCIM] disconnect];
        [self.navigationController popViewControllerAnimated:NO];
        
    }];
    [alert addAction:action1];
    [self presentViewController:alert animated:YES completion:nil];
}

//设置用户信息
- (void)getUserInfoWithUserId:(NSString *)userId
                   completion:(void (^)(RCUserInfo *userInfo))completion{
    RCUserInfo *userinfo = [RCUserInfo new];
    
    [AVQuery doCloudQueryInBackgroundWithCQL:@"select * from _User where objectId = ?" pvalues:@[userId] callback:^(AVCloudQueryResult *result, NSError *error) {
        AVUser *user = result.results[0];
        userinfo.userId = user.objectId;
        userinfo.name = user.username;
        AVFile *file1 = [user objectForKey:@"headImage"];
        userinfo.portraitUri = file1.url;
        return completion(userinfo);
    }];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
